package com.zhiyou100.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou100.model.Classity;
import com.zhiyou100.model.PublishTheProject;
import com.zhiyou100.service.ClassityService;
import com.zhiyou100.util.FormatDate;

@Controller
@RequestMapping("/classity")
public class ClassityController {

	@Autowired
	private ClassityService classityService;
	
	// 查询全部项目分类
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public String list(@RequestParam(defaultValue="1")int pageNo,Model model) {
		// 使用pageHelper 进行分页 
		// 第一个参数代表当前页,第二个参数代表每页的数据记录条数
		PageHelper.startPage(pageNo, 2);
		System.out.println("查询全部,来到了clasity----Controller=========");
		List<Classity> list =classityService.list();
		System.out.println("输出查询到的全部项目:"+list);
		// 查询到全部数据后,开始分页
		PageInfo<Classity> pageInfo = new PageInfo<Classity>(list);
		// 把分页后的数据(list结果集)添加进model域   并且把pageInfo对象,添加进model域
		model.addAttribute("list", pageInfo.getList());
		model.addAttribute("page",pageInfo);
		return "forward:/admin/classify/all_classity.jsp";
	}
	
	// 查询项目分类详情
	@RequestMapping(value="/detail",method=RequestMethod.GET)
	public String detail(int id,Model model) {
		Classity classity = classityService.detail(id);
		model.addAttribute("classity", classity);
		System.out.println("输出查询到的项目分类详情:"+classity);
		return "forward:/admin/classify/all_classity.jsp";
	}
	
//	// 更新项目分类详情
//	// 1.获取需要更新的项目分类详情
//	@RequestMapping(value="/detail",method=RequestMethod.GET)
//	public String update(int id,Model model) {
//		Classity classity = classityService.detail(id);
//		model.addAttribute("classity", classity);
//		System.out.println("输出查询到的项目分类详情:"+classity);
//		return "forward:/admin/classify/all_classity.jsp";
//	}
	// 2.把更改后的项目分类详情插入到数据库
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public String update(Classity classity) {
		classityService.update(classity);
		return "forward:/classity/list";
	}
	
	// 添加项目分类详情
	@RequestMapping(value="/add",method=RequestMethod.GET)
	public String add(Classity classity) {
		classity.setAddtime(new Date());
		classity.setState(1);
		System.out.println("添加的项目分类详情"+classity);
		classityService.add(classity);
		return "forward:/classity/list";
	}
	
	// 删除项目分类详情
	@RequestMapping(value="/delete",method=RequestMethod.GET)
	public String delete(int id) {
		classityService.delete(id);
		return "forward:/classity/list";
	}
}
